Public Class [Function]
    Inherits ObjectBase

    Private msName As String
    Private mlOID As Long
    Private mlOwnerID As Long
    Private mlDatabaseID As Long
    Private mlDatabaseRevID As Long
    Private msDesc As String
    Private msRevDate As String = Now

    Private msTextHash As String

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal lID As Long)
        MyBase.New(lID)
    End Sub

    Public Sub New(ByVal lID As Long, ByVal lRevID As Long)
        MyBase.New(lID, lRevID)
    End Sub

#Region "Properties"
    Public Property Name() As String
        Get
            Return msName
        End Get
        Set(ByVal value As String)
            msName = value
        End Set
    End Property

    Public Property OID() As Long
        Get
            Return mlOID
        End Get
        Set(ByVal value As Long)
            mlOID = value
        End Set
    End Property

    Public Property OwnerID() As Long
        Get
            Return mlOwnerID
        End Get
        Set(ByVal value As Long)
            mlOwnerID = value
        End Set
    End Property

    Public Property DatabaseID() As Long
        Get
            Return mlDatabaseID
        End Get
        Set(ByVal value As Long)
            mlDatabaseID = value
        End Set
    End Property

    Public Property DatabaseRevID() As Long
        Get
            Return mlDatabaseRevID
        End Get
        Set(ByVal value As Long)
            mlDatabaseRevID = value
        End Set
    End Property

    Public Property RevDate() As String
        Get
            Return msRevDate
        End Get
        Set(ByVal value As String)
            msRevDate = value
        End Set
    End Property

    Public Property Desc() As String
        Get
            Return msDesc
        End Get
        Set(ByVal value As String)
            msDesc = value
        End Set
    End Property

    Public Property TextHash() As String
        Get
            Return msTextHash
        End Get
        Set(ByVal value As String)
            msTextHash = value
        End Set
    End Property
#End Region

#Region "database"
    Public Sub Load()
        Dim oObject As New DBFunction

        If mlRevID >= 0 Then
            oObject.LoadRev(Me, mlRevID)
        Else
            oObject.Load(Me)
        End If

    End Sub

    Public Sub Save(Optional ByVal bForceAdd As Boolean = False)
        Dim oObject As New DBFunction

        If bForceAdd = False AndAlso mbObjectExists = True Then
            oObject.Update(Me)
        Else

            Dim oNextID As New DBNextIDValue("Function")
            Dim lNextID As Long = oNextID.NextValue

            Me.ID = lNextID

            If bForceAdd = False Then
                Me.RevID = 0
            End If

            Me.CreateDate = Now
            oObject.Add(Me)
            mbObjectExists = True
        End If
    End Sub

    Public Sub Rev(ByVal sTextHash As String)
        Dim oObject As New DBFunction

        Me.RevDate = Now

        If Me.TextHash > "" Then
            mlRevID = mlRevID + 1
            Me.TextHash = sTextHash

            Me.CreateDate = Now
            oObject.Add(Me)
        Else
            Me.TextHash = sTextHash
            oObject.Update(Me)

        End If

        mbObjectExists = True
    End Sub

    Public Sub ForceRev()
        Dim oObject As New DBFunction

        Me.TextHash = ""

        Scan()
    End Sub

    Public Sub Delete()
        Dim oObject As New DBFunction

        oObject.Delete(Me)
        mbObjectExists = False
    End Sub
#End Region

    Public Function Scan() As Boolean
        Dim bFunctionHasChanged As Boolean = False

        '---Scan text of this sproc 
        Dim oDatabase As New SQLAutoDocLib.Database(mlDatabaseID)
        oDatabase.Load()

        Dim oServer As New SQLAutoDocLib.Server(oDatabase.ServerID)
        oServer.Load()

        Dim oLogin As New SQLAutoDocLib.Login(mlOwnerID)
        oLogin.Load()

        '---Get text of view and checksum
        Dim oTextScanner As New DBScanFunctionText(oServer, oDatabase, oLogin, Me)
        Dim sFunctionText As String = ""
        Dim sTextHash As String = ""
        oTextScanner.Scan(sFunctionText, sTextHash)

        '---If checksum has changed, then rev function
        If sTextHash <> msTextHash Then

            bFunctionHasChanged = True

            '---Function has revved
            Me.Rev(sTextHash)

            '---Save text
            Dim oFTextFactory As New SQLAutoDocLib.FunctionText_Factory
            oFTextFactory.SaveText(Me, sFunctionText)
        End If

        Return bFunctionHasChanged
    End Function
End Class
